--[[
    Author          ZhengFeng
    Time            2021.03.05
    Description     英雄-专武-升阶属性界面
]]--

require "Common/define"
local UIWeaponLevelUp = UIBase.New(PanelResNames.UIWeaponLevelUp)
local this = UIWeaponLevelUp



function this.Init(closeFunc)
    this.goFunc = closeFunc;
    UIBase.Init(this)
end

function this:OnCreate()
    self.Btn_BGClose = self:GetChild("Btn_BGClose")
    self:RegisterOnClick(self.Btn_BGClose,self.onClickClose)

    -- self.Btn_LevelUp = self:GetChild("Btn_LevelUp")
    -- self:RegisterOnClick(self.Btn_LevelUp,self.onClickLevelUp)
    -- self.Btn_UnLevelUp = self:GetChild("Btn_UnLevelUp")
    -- self:RegisterOnClick(self.Btn_UnLevelUp,self.onClickUnLevelUp)
    -- self.Controller_LevelUp = self:GetController("levelUp")

    -- self.Img_CostIcon = self:GetChild("Img_CostIcon")
    -- self.Txt_CostNumber = self:GetChild("Txt_CostNumber")
     self.LevelUpController = self:GetController("state")
    
    -- this.btnSource = self:GetChild("btnSource")
    -- this.effectController = this.btnSource:GetController("effect")
    -- self:RegisterOnClick(this.btnSource, this.onClickSource)
    -- this.sourceUIobj = self:GetChild("UISourceMultiple");
    -- this.showSourceController = self:GetController("showSource")

    self.List_WeaponAttribute = self:GetChild("List_WeaponAttribute")
    self.List_WeaponAttribute.itemRenderer = this.rendererWeaponAttributeItem
end

function this:OnEnable()
    ListenEvent(Config.EventType.HeroWeapon_LevelUp,this.dispatchWeaponLevelUp)
   -- ListenEvent(Config.EventType.Fresh_Bag,this.updateSource)
    this:refreshPanel();
    this:onClickLevelUp();
end

function this:OnDisable()
    CloseEvent(Config.EventType.HeroWeapon_LevelUp, this.dispatchWeaponLevelUp)
   -- CloseEvent(Config.EventType.Fresh_Bag,this.updateSource)
end
function this:OnDestroy()
    
end

function this:onClickClose()
    closeUI(PanelResNames.UIWeaponLevelUp)
end

function this:onClickLevelUp()--升级
    if self.currentWeaponLv > self.maxWeaponLv
        or self.weaponNeedStarArr[self.currentWeaponLv ] > self.currentPartnerStar then
        UISysTips.AddMsg(string.format(getLanguage("Sys_WeaponCantRankup"),EnumConst.StarType[self.weaponNeedStarArr[self.currentWeaponLv + 1]/10]))
        return
    end
    weaponHandler.sendWeaponUpLevelMessage(HeroManager.SelectedWeaponPartnerId, self.currentWeaponLv-1)
end

function this:onClickUnLevelUp()
    UISysTips.AddMsg(getLanguage("Weapon_CannotLevelUp"))
end

function this.dispatchWeaponLevelUp()
    this:refreshPanel()
end

function this:refreshPanel()
    local _currentPartnerInfo = HeroManager.getHeroDatabyPartnerId(HeroManager.SelectedWeaponPartnerId,true)
    local _configId = 150000 + tonumber(_currentPartnerInfo.partnerModelId)
    local _weaponConfig = G.dataTable["t_weapon"][tostring(_configId)]
    local _currentWeaponInfo = EquipManager.tabWeapon[tostring(_currentPartnerInfo.partnerId)]
    if _currentWeaponInfo == nil then
        printError("UIWeaponLevelUp currentWeaponInfo is Null, partnerId ".._currentPartnerInfo.partnerId)
        this:onClickClose()
        return
    end

    self.currentPartnerStar = _currentPartnerInfo.star
    self.currentWeaponLv = _currentWeaponInfo.level + 1
    self.maxWeaponLv = _weaponConfig.f_MaxLevel
    self.weaponNeedStarArr = decodeJsonStrToTabel(_weaponConfig.f_WeaponNeedStar)
    if _currentWeaponInfo.attiredId == 0 then
        local _weaponShowConfig = decodeJsonStrToTabel(_weaponConfig.f_WeaponShow)
        _currentWeaponInfo.attiredId = _weaponShowConfig[1][2]
    end
    
    local _weaponValue = decodeJsonStrToTabel(_weaponConfig.f_WeaponValue)
    local _currentWeaponAttribute = _weaponValue[self.currentWeaponLv-1]

    local _currentWeaponEnergyCost = decodeJsonStrToTabel(_weaponConfig.f_WeaponEnergyCost)
    local _levelUpCost = _currentWeaponEnergyCost[self.currentWeaponLv]
    local _levelUpItemConfig = ItemManager.getItemConstDatabyModelId(_levelUpCost[1])
  --  self.Img_CostIcon.url = EnumConst.AtlasPath.ItemAtlas .. _levelUpItemConfig.icon   

    if self.currentWeaponLv <= self.maxWeaponLv then
        --激活下一级
        this.costId = _levelUpCost[1]
        local _myselfItemNumber = ItemManager.getCurrencyCountByModelId(_levelUpCost[1])
        local _costStr = getNumString(_myselfItemNumber)
        local _levelUpState = 0
        if _myselfItemNumber < _levelUpCost[2] then--判断材料是否充足
            _costStr = string.TextColor(_costStr,"#FF0000")
            _levelUpState = 1
            -- if not logicMgr.ItemManager.getSourceStart("UIWeapon") then
            --     this.effectController.selectedIndex = 1;
            -- end
        end
        _costStr = _costStr .. "/" .. getNumString(_levelUpCost[2])
       -- self.Txt_CostNumber.text = _costStr
        --self.Controller_LevelUp.selectedIndex = _levelUpState
        --self.Btn_LevelUp.enabled = _touchable
        --self.Btn_LevelUp.touchable = _touchable
        local _levelUpStatus = 1
        if self.currentWeaponLv == 1 then
            _levelUpStatus = 0
        end
        self.LevelUpController.selectedIndex = _levelUpStatus
        this.currentWeaponLevel = _currentWeaponInfo.level

        --属性列表
        local _nextWeaponAttribute = _weaponValue[self.currentWeaponLv]
        self.attributeArr = {}
        for _, nextAttribute in ipairs(_nextWeaponAttribute) do
            local _attributeData = {}
            _attributeData.nextAttributeId = nextAttribute[1]
            local _nextAttributeLibrary = G.dataTable["t_attributelibrary"][tostring(_attributeData.nextAttributeId)]
            _attributeData.attributeName = _nextAttributeLibrary.f_AttributeName
            _attributeData.nextAttributeNumber = nextAttribute[2]

            local _attributeNumberStr = ""
            if _nextAttributeLibrary.f_AttributeType == 1 then
                _attributeNumberStr = string.format("+%d",_attributeData.nextAttributeNumber)
            elseif _nextAttributeLibrary.f_AttributeType == 2 then
                _attributeNumberStr = string.format("+%s%%",tostring(_attributeData.nextAttributeNumber/100))
            end
            _attributeData.nextAttributeStr = _attributeNumberStr
            _attributeData.newStatus = 1

            --当前属性
            if _currentWeaponAttribute==nil then
                _attributeData.currentAttributeStr=string.format("+%s%%",tostring(0/100))
            else
                for _, currentAttribute in ipairs(_currentWeaponAttribute) do
                    if _attributeData.nextAttributeId == currentAttribute[1] then
                         _attributeData.currentAttributeNumber = tonumber(currentAttribute[2])
                        _attributeNumberStr = "+0%"
                        _attributeData.newStatus = 1;
                        if _attributeData.currentAttributeNumber > 0 then
                            _attributeData.newStatus = 0
                            if _nextAttributeLibrary.f_AttributeType == 1 then
                                 _attributeNumberStr = string.format("+%d",_attributeData.currentAttributeNumber)
                             elseif _nextAttributeLibrary.f_AttributeType == 2 then
                                 _attributeNumberStr = string.format("+%s%%",tostring(_attributeData.currentAttributeNumber/100))
                             end
                        end
                        _attributeData.currentAttributeStr = _attributeNumberStr
                        break
                    end
                 end
            end
           
            table.insert(self.attributeArr,_attributeData)
        end
        self.List_WeaponAttribute.numItems = #self.attributeArr + 1
    else
        --满级
        this:onClickClose()
    end
end

function this.rendererWeaponAttributeItem(index, obj)
    if index == 0 then
        -- if this.currentWeaponLevel == 0 then
        --     obj:GetChild("Txt_NewTag").text = ""
        --     obj:GetController("status").selectedIndex = 1
        -- else
        --     obj:GetController("status").selectedIndex = 0
        -- end
        obj:GetChild("attrText").text = getLanguage("Sys_HeroLevel")
        obj:GetChild("num").text = this.currentWeaponLevel-1
        obj:GetChild("upNum").text = this.currentWeaponLevel
        return
    end
    local _attributeInfo = this.attributeArr[index]
    -- obj:GetController("status").selectedIndex = _attributeInfo.newStatus
    -- if _attributeInfo.newStatus == 1 then
    --     obj:GetChild("Txt_NewTag").text = "New"
    -- end
    HelperFunc.SetIcon(obj:GetChild("loader_icon"),"attr".._attributeInfo.nextAttributeId,EnumConst.ItemShowType.ICON)
    obj:GetChild("attrText").text = _attributeInfo.attributeName
    obj:GetChild("num").text = _attributeInfo.currentAttributeStr
    obj:GetChild("upNum").text = _attributeInfo.nextAttributeStr
end

-- function this.onClickSource()
--     this.showSourceController.selectedIndex = 1;
--     this.showSourceList = {this.costId}
--     logicMgr.ItemManager.setSourceStart("UIWeapon", true)
--     this.effectController.selectedIndex = 0;
--     this.updateSource()
-- end
-- function this.updateSource()
--     logicMgr.ItemManager.updateSourceUI(this.sourceUIobj, this.showSourceList, function ()
--         this.showSourceController.selectedIndex = 0
--     end, function ()
--         this.showSourceController.selectedIndex = 0
--         this:onClickClose()
--         if this.goFunc then
--             this.goFunc();
--         end
--     end );
-- end
return this